package com.chen.shopping_manager_api.controller;

import co.elastic.clients.elasticsearch.xpack.usage.Base;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.shopping_common.pojo.Category;
import com.chen.shopping_common.result.BaseResult;
import com.chen.shopping_common.service.CategoryService;
import istio.v1.auth.Ca;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @DubboReference
    private CategoryService categoryService;

    @PostMapping("/add")
    public BaseResult add(@RequestBody Category category){
        categoryService.add(category);
        return BaseResult.ok();
    }

    @PutMapping("/update")
    public BaseResult update(@RequestBody Category category){
        categoryService.update(category);
        return BaseResult.ok();
    }

    @DeleteMapping("/delete")
    public BaseResult delete(Long[] ids){
        categoryService.delete(ids);
        return BaseResult.ok();
    }

    @GetMapping("/findById")
    public BaseResult<Category> findById(Long id){
        Category category = categoryService.findById(id);
        return BaseResult.ok(category);
    }

    @PutMapping("/updateStatus")
    public BaseResult updateStatus(Long id,Integer status){
        categoryService.updateStatus(id, status);
        return BaseResult.ok();
    }

    @GetMapping("/search")
    public BaseResult<Page<Category>> search(int page,int size){
        Page<Category> page1 = categoryService.findByPage(page, size);
        return BaseResult.ok(page1);
    }

    @GetMapping("/findAll")
    public BaseResult<List<Category>> findAll(){
        List<Category> all = categoryService.findAll();
        return BaseResult.ok(all);
    }
}
